package br.com.rampage.controller.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.rampage.model.bo.DependenteBO;
import br.com.rampage.model.bo.ReturnCallBack;

/**
 * Servlet implementation class CadastroDependenteServlet
 */
@WebServlet("/CadastroDependenteServlet")
public class CadastroDependenteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CadastroDependenteServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String nome = request.getParameter("nome");
		String cpf = request.getParameter("cpf");
		String sexo = request.getParameter("sexo");
		String data_nascimento = request.getParameter("data_nascimento");

		ReturnCallBack callBack = new DependenteBO().cadastrar(nome, sexo, cpf, data_nascimento);

		if (callBack.isSucesso()) {
			response.setContentType("text/html;charset-UTF-8");
			PrintWriter writer = response.getWriter();
			new Message().print(writer, TypeReturnServlet.success,
					callBack.getMsgReturn());

			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/cadastro_dependente.jsp");
			dispatcher.include(request, response);
		}else{
			
			response.setContentType("text/html;charset-UTF-8");
			PrintWriter writer = response.getWriter();
			new Message().print(writer, TypeReturnServlet.danger, callBack.getMsgReturn());

			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/cadastro_dependente.jsp");
			dispatcher.include(request, response);;
			
		}
	}

}
